package cn.com.ghy.android.style;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MenuTabContent extends PopupWindow {
	private GridView gvBody = null;
	private LinearLayout mLayout = null;

	public MenuTabContent(Context context, OnItemClickListener bodyClick, int colorBgTabMenu,
			int aniTabMenu) {
		super(context);

		mLayout = new LinearLayout(context);
		mLayout.setOrientation(LinearLayout.VERTICAL);

		gvBody = new GridView(context);
		gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gvBody.setNumColumns(4);
		gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		gvBody.setVerticalSpacing(10);
		gvBody.setHorizontalSpacing(10);
		gvBody.setPadding(10, 10, 10, 10);
		gvBody.setGravity(Gravity.CENTER);
		gvBody.setOnItemClickListener(bodyClick);
		mLayout.addView(gvBody);

		this.setContentView(mLayout);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));
		this.setAnimationStyle(aniTabMenu);
		this.setFocusable(true);
	}

	public void SetBodySelect(int index, int colorSelBody) {
		int count = gvBody.getChildCount();
		for (int i = 0; i < count; i++) {
			if (i != index)
				((LinearLayout) gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);
		}
		((LinearLayout) gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);
	}

	public void SetBodyAdapter(MenuBodyAdapter bodyAdapter) {
		gvBody.setAdapter(bodyAdapter);
	}

	static public class MenuBodyAdapter extends BaseAdapter {
		private Context mContext;
		private int fontColor, fontSize;
		private String[] texts;
		private int[] resID;

		public MenuBodyAdapter(Context context, String[] texts, int[] resID, int fontSize,
				int fontColor) {
			this.mContext = context;
			this.fontColor = fontColor;
			this.texts = texts;
			this.fontSize = fontSize;
			this.resID = resID;
		}

		public int getCount() {
			return texts.length;
		}

		public Object getItem(int position) {

			return makeMenuBody(position);
		}

		public long getItemId(int position) {
			return position;
		}

		private LinearLayout makeMenuBody(int position) {
			LinearLayout result = new LinearLayout(this.mContext);
			result.setOrientation(LinearLayout.VERTICAL);
			result.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
			result.setPadding(0, 0, 0, 0);

			TextView text = new TextView(this.mContext);
			text.setText(texts[position]);
			text.setTextSize(fontSize);
			text.setTextColor(fontColor);
			text.setGravity(Gravity.CENTER);
			text.setPadding(0, 0, 0, 0);
			ImageView img = new ImageView(this.mContext);
			img.setBackgroundResource(resID[position]);
			result.addView(img, new LinearLayout.LayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));
			result.addView(text);
			return result;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return makeMenuBody(position);
		}
	}
}
